package org.lc.service.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.lc.platform.base.vo.SelectListVo;
import org.lc.platform.mybatis.crud.service.impl.CrudBaseServiceImpl;
import org.lc.platform.mybatis.manager.query.QueryBuilder;
import org.lc.service.system.dto.SysRoleDto;
import org.lc.service.system.entity.SysRole;
import org.lc.service.system.mapper.SysRoleMapper;
import org.lc.service.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysRoleServiceImpl extends CrudBaseServiceImpl<SysRoleDto, SysRole, SysRoleMapper, SysRole> implements SysRoleService {

    @Value("${properties.common.init-system.default-role:''}")
    private String defaultRole;

    @Override
    public SysRole Convertor(SysRole data) {
        return data;
    }

    @Override
    public QueryBuilder<SysRole> getQueryBuilder(SysRoleDto query, QueryBuilder<SysRole> queryBuilder) {
        queryBuilder.like(query.getName() != null, "name", query.getName());
        if (!defaultRole.isEmpty()) {
            queryBuilder.ne("code", defaultRole);
        }
        return queryBuilder;
    }

    @Override
    public boolean getRoleExits(String role) {
        var warp = new LambdaQueryWrapper<SysRole>();
        warp.eq(role != null, SysRole::getCode, role);
        return this.baseMapper.exists(warp);
    }

    @Override
    public List<SelectListVo> getRoleSelectList() {
        return this.baseMapper.findRoleSelectList(defaultRole);
    }
}